/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatNI;

import ChatSystem.MessageFactory;
import common.*;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/**
 *
 * @author gaye
 */
public class UDPCom {

    /* Attributs */
    public DatagramSocket datagrameSocket; //envoie et reception des datagrames
    public int portNum;
    public int destinationPort;
    public InetAddress destinationIP;

    /* Le constructeur */
    public UDPCom(int destinationPort, InetAddress destinationIP) throws SocketException {
        super();

        this.destinationPort = destinationPort;
        this.destinationIP = destinationIP;
        this.datagrameSocket = new DatagramSocket();
    }

    public void sendHello(String username, boolean reply) {
        Hello msgHello = ChatSystem.MessageFactory.createHello(username, reply);
        byte[] mess = MessageFactory.toByte(msgHello);
        DatagramPacket packet = new DatagramPacket(mess, mess.length, destinationIP, destinationPort);
        try {
            this.datagrameSocket.send(packet);
        } catch (IOException ex) {
            System.out.println("Error sending datagram");
        }

    }

    public void sendBye() {
        Bye msgBye = ChatSystem.MessageFactory.createBye();
        byte[] mess = MessageFactory.toByte(msgBye);
        DatagramPacket packet = new DatagramPacket(mess, mess.length, destinationIP, destinationPort);
        try {
            this.datagrameSocket.send(packet);

        } catch (IOException ex) {
            System.out.println("Error sending datagram");
        }
    }

    public void sendMess(String messtxt) {
        Text msgtxt = ChatSystem.MessageFactory.createText(messtxt);
        byte[] mess = MessageFactory.toByte(msgtxt);
        DatagramPacket packet = new DatagramPacket(mess, mess.length, destinationIP, destinationPort);
        try {
            this.datagrameSocket.send(packet);

        } catch (IOException ex) {
            System.out.println("Error sending datagram");
        }
    }

    public void sendFileReq(String nom, int taille) {
        TransfertFileRequest req = ChatSystem.MessageFactory.createTransfertFileRequest(nom, taille);
        byte[] mess = MessageFactory.toByte(req);
        DatagramPacket packet = new DatagramPacket(mess, mess.length, destinationIP, destinationPort);
        try {
            this.datagrameSocket.send(packet);

        } catch (IOException ex) {
            System.out.println("Error sending datagram");
        }
    }

    public void sendFileAcc(Boolean accept) {
        TransfertFileAccept acc = ChatSystem.MessageFactory.createTransfertFileAccept(accept);
        byte[] mess = MessageFactory.toByte(acc);
        DatagramPacket packet = new DatagramPacket(mess, mess.length, destinationIP, destinationPort);
        try {
            this.datagrameSocket.send(packet);

        } catch (IOException ex) {
            System.out.println("Error sending datagram");
        }
    }
}
